package com.lk.controller;

import com.lk.bean.Admin;
import com.lk.bean.Student;
import com.lk.bean.Teacher;
import com.lk.service.AdminService;
import com.lk.service.StudentService;
import com.lk.service.TeacherService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/common")
public class CommonController {

    @Resource
    private AdminService adminService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentService studentService;

    /**
     * 跳转到修改密码页面
     *
     * @return
     */
    @GetMapping("/goSettingView")
    public String goSettingView(){
        return "/common/settings";
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @PostMapping("/editPassword")
    @ResponseBody
    public Map<String,Object> editPassword(String oldPassword, String newPassword, HttpSession session){
        Map<String,Object> result = new HashMap<>();

        //判断当前登录用户类型
        int userType = Integer.parseInt(session.getAttribute("userType").toString());
        if (userType == 1){
            Admin admin = (Admin)session.getAttribute("userInfo");
            Integer id = admin.getId();
            //判断旧密码输入是否正确
            if (!admin.getPassword().equals(oldPassword)){
                result.put("success",false);
                result.put("msg","原密码输入错误");
            }else {
                int row = adminService.updatePassWord(id,newPassword);
                if (row == 1){
                    result.put("success",true);
                }else {
                    result.put("success",false);
                    result.put("msg","密码修改失败");
                }
            }
        }
        if (userType == 2){
            Student student = (Student) session.getAttribute("userInfo");
            Integer id = student.getId();
            //判断旧密码输入是否正确
            if (!student.getPassword().equals(oldPassword)){
                result.put("success",false);
                result.put("msg","原密码输入错误");
            }else {
                int row = studentService.updatePassWord(id,newPassword);
                if (row == 1){
                    result.put("success",true);
                }else {
                    result.put("success",false);
                    result.put("msg","密码修改失败");
                }
            }
        }
        if (userType == 3){
            Teacher teacher = (Teacher) session.getAttribute("userInfo");
            Integer id = teacher.getId();
            //判断旧密码输入是否正确
            if (!teacher.getPassword().equals(oldPassword)){
                result.put("success",false);
                result.put("msg","原密码输入错误");
            }else {
                int row = teacherService.updatePassWord(id,newPassword);
                if (row == 1){
                    result.put("success",true);
                }else {
                    result.put("success",false);
                    result.put("msg","密码修改失败");
                }
            }
        }
        return result;
    }


}
